public with sharing class ArinsoSurveyWorkflow {

    public static void CustomerSurveyResponseInsert(List <Customer_Satisfaction_Survey__c> lObjects){
    	List<Customer_Satisfaction_Survey__c> lCustomerSatisfactionSurvey = new List<Customer_Satisfaction_Survey__c>();
        for(Customer_Satisfaction_Survey__c customerSurvey:lObjects){
            
            Customer_Satisfaction_Survey__c customerSurvey_new = [Select c.Contact__r.AccountId,c.Manager_Email__c From Customer_Satisfaction_Survey__c c where c.Id =: customerSurvey.Id];
            
            //Duplicate record validation
            List <Customer_Satisfaction_Survey__c> duplicateSurvey = [Select c.Id From Customer_Satisfaction_Survey__c c where c.Survey_Year__c =:customerSurvey.Survey_Year__c and c.Survey_Month__c =:customerSurvey.Survey_Month__c and c.Contact__c =:customerSurvey.Contact__c];
            if(duplicateSurvey.size() >= 2){
                customerSurvey.addError('Duplicate survey record for the selected customer, month and year');
                continue; 
            }
            
            if(customerSurvey_new.Contact__r == null || customerSurvey_new.Contact__r.AccountId == null){
                continue;           
            }
            
            //Add the manager email
            Account account = [Select a.Owner.Id From Account a where a.Id =: customerSurvey_new.Contact__r.AccountId];
            
            if(account == null){
            	lCustomerSatisfactionSurvey.add(customerSurvey_new);
                continue;           
            }
            
            User user_vcmanager = [Select u.Manager.Email From User u where u.Id =: account.Owner.Id];
    
            if(user_vcmanager == null || user_vcmanager.Manager.Email == null){
            	lCustomerSatisfactionSurvey.add(customerSurvey_new);
                continue;           
            }
    
            customerSurvey_new.Manager_Email__c = user_vcmanager.Manager.Email;
                
            lCustomerSatisfactionSurvey.add(customerSurvey_new);
        }   
        update lCustomerSatisfactionSurvey;
    }

    public static void CustomerSurveyResponseUpdated(List <Customer_Satisfaction_Survey__c> lObjects){
    	
        for(Customer_Satisfaction_Survey__c customerSurvey:lObjects){

            //Duplicate record validation
            List <Customer_Satisfaction_Survey__c> duplicateSurvey = [Select c.Id From Customer_Satisfaction_Survey__c c where c.Survey_Year__c =:customerSurvey.Survey_Year__c and c.Survey_Month__c =:customerSurvey.Survey_Month__c and c.Contact__c =:customerSurvey.Contact__c and c.Id !=:customerSurvey.Id];
            if(duplicateSurvey.size() >= 1){
                customerSurvey.addError('Duplicate survey record for the selected customer, month and year');
            }

            //Check Customer alert status
            if(customerSurvey.CustomerResponse1__c != null){
            	
            	Double CurrentMonthTopBox = 0;
            	Double CurrentMonthTop2Box = 0;
            	Double CurrentMonthNeutralBox = 0;
            	Double CurrentMonthBottomBox = 0;
            	
            	Integer totalAnswered = 7;
            	
                Double dblResponseTotal = customerSurvey.CustomerResponse1__c;
                if (customerSurvey.CustomerResponse1__c == 5) {
                	CurrentMonthTopBox++;
                }
                if (customerSurvey.CustomerResponse1__c >= 4) {
                	CurrentMonthTop2Box++;
                }
                if (customerSurvey.CustomerResponse1__c == 3) {
                	CurrentMonthNeutralBox++;
                }
                if (customerSurvey.CustomerResponse1__c <= 2) {
                	CurrentMonthBottomBox++;
                }
                
                dblResponseTotal += customerSurvey.CustomerResponse2__c;
                if (customerSurvey.CustomerResponse2__c == 5) {
                	CurrentMonthTopBox++;
                }
                if (customerSurvey.CustomerResponse2__c >= 4) {
                	CurrentMonthTop2Box++;
                }
                if (customerSurvey.CustomerResponse2__c == 3) {
                	CurrentMonthNeutralBox++;
                }
                if (customerSurvey.CustomerResponse2__c <= 2) {
                	CurrentMonthBottomBox++;
                }
                
                              
                dblResponseTotal += customerSurvey.CustomerResponse3__c;
                if (customerSurvey.CustomerResponse3__c == 5) {
                	CurrentMonthTopBox++;
                }
                if (customerSurvey.CustomerResponse3__c >= 4) {
                	CurrentMonthTop2Box++;
                }
                if (customerSurvey.CustomerResponse3__c == 3) {
                	CurrentMonthNeutralBox++;
                }
                if (customerSurvey.CustomerResponse3__c <= 2) {
                	CurrentMonthBottomBox++;
                }
                              
                dblResponseTotal += customerSurvey.CustomerResponse4__c;
                if (customerSurvey.CustomerResponse4__c == 5) {
                	CurrentMonthTopBox++;
                }
                if (customerSurvey.CustomerResponse4__c >= 4) {
                	CurrentMonthTop2Box++;
                }
                if (customerSurvey.CustomerResponse4__c == 3) {
                	CurrentMonthNeutralBox++;
                }
                if (customerSurvey.CustomerResponse4__c <= 2) {
                	CurrentMonthBottomBox++;
                }
                
                dblResponseTotal += customerSurvey.CustomerResponse5__c;
                if (customerSurvey.CustomerResponse5__c == 5) {
                	CurrentMonthTopBox++;
                }
                if (customerSurvey.CustomerResponse5__c >= 4) {
                	CurrentMonthTop2Box++;
                }
                if (customerSurvey.CustomerResponse5__c == 3) {
                	CurrentMonthNeutralBox++;
                }
                if (customerSurvey.CustomerResponse5__c <= 2) {
                	CurrentMonthBottomBox++;
                }
                              
				try	{                
                	dblResponseTotal += Double.valueOf(customerSurvey.CustomerResponse6__c);
	                if (Double.valueOf(customerSurvey.CustomerResponse6__c) == 5) {
	                	CurrentMonthTopBox++;
	                }
	                if (Double.valueOf(customerSurvey.CustomerResponse6__c) >= 4) {
	                	CurrentMonthTop2Box++;
	                }
	                if (Double.valueOf(customerSurvey.CustomerResponse6__c) == 3) {
	                	CurrentMonthNeutralBox++;
	                }
	                if (Double.valueOf(customerSurvey.CustomerResponse6__c) <= 2) {
	                	CurrentMonthBottomBox++;
	                }                	
	                
	                totalAnswered++;
	                
				}	catch(Exception e){}
                
                dblResponseTotal += customerSurvey.CustomerResponse7__c;
                if (customerSurvey.CustomerResponse7__c == 5) {
                	CurrentMonthTopBox++;
                }
                if (customerSurvey.CustomerResponse7__c >= 4) {
                	CurrentMonthTop2Box++;
                }
                if (customerSurvey.CustomerResponse7__c == 3) {
                	CurrentMonthNeutralBox++;
                }
                if (customerSurvey.CustomerResponse7__c <= 2) {
                	CurrentMonthBottomBox++;
                }
                                
                dblResponseTotal += customerSurvey.CustomerResponse8__c;
                if (customerSurvey.CustomerResponse8__c == 5) {
                	CurrentMonthTopBox++;
                }
                if (customerSurvey.CustomerResponse8__c >= 4) {
                	CurrentMonthTop2Box++;
                }
                if (customerSurvey.CustomerResponse8__c == 3) {
                	CurrentMonthNeutralBox++;
                }
                if (customerSurvey.CustomerResponse8__c <= 2) {
                	CurrentMonthBottomBox++;
                }

				
				try{                
                	dblResponseTotal += Double.valueOf(customerSurvey.CustomerResponse9__c);
	                if (Double.valueOf(customerSurvey.CustomerResponse9__c) == 5) {
	                	CurrentMonthTopBox++;
	                }
	                if (Double.valueOf(customerSurvey.CustomerResponse9__c) >= 4) {
	                	CurrentMonthTop2Box++;
	                }
	                if (Double.valueOf(customerSurvey.CustomerResponse9__c) == 3) {
	                	CurrentMonthNeutralBox++;
	                }
	                if (Double.valueOf(customerSurvey.CustomerResponse9__c) <= 2) {
	                	CurrentMonthBottomBox++;
	                }
                	
                	totalAnswered++;
				}	catch(Exception e){}                
                
                customerSurvey.CustomerResponseTotal__c = dblResponseTotal.intValue();
                customerSurvey.CustomerResponseAverage__c = ((dblResponseTotal/totalAnswered))*(20);
                customerSurvey.CustomerResponseAverageAbsolute__c = dblResponseTotal/totalAnswered;
                
                customerSurvey.Current_Month_Top_Box__c = (CurrentMonthTopBox/totalAnswered)*(100);
                customerSurvey.Current_Month_Top_2_Box__c = (CurrentMonthTop2Box/totalAnswered)*(100);
                customerSurvey.Current_Month_Neutral_Box__c = (CurrentMonthNeutralBox/totalAnswered)*(100);
                customerSurvey.Current_Month_Bottom_Box__c = (CurrentMonthBottomBox/totalAnswered)*(100);
                
                System.debug('ABSILOG #################  ArinsoSurveyWorkflow dblResponseTotal ' +  dblResponseTotal + ' totalAnswered ' + totalAnswered + '##########');
                
				
            }
        
           //Check VCM Response Status
           if(customerSurvey.VCMresponse1__c != null){
           		Integer totalAnswered = 7;
                Double dblResponseTotal = customerSurvey.VCMresponse1__c;
                dblResponseTotal += customerSurvey.VCMresponse2__c;
                dblResponseTotal += customerSurvey.VCMresponse3__c;
                dblResponseTotal += customerSurvey.VCMresponse4__c;
                dblResponseTotal += customerSurvey.VCMresponse5__c;
                try{
                	dblResponseTotal += Double.valueOf(customerSurvey.VCMresponse6__c);
                	totalAnswered++;
                }catch(Exception e){}
                dblResponseTotal += customerSurvey.VCMresponse7__c;
                dblResponseTotal += customerSurvey.VCMresponse8__c;
                try{
                	dblResponseTotal += Double.valueOf(customerSurvey.VCMresponse9__c);
                	totalAnswered++;
                }catch(Exception e){}                
                
                customerSurvey.VCMresponseTotal__c = dblResponseTotal.intValue();
                customerSurvey.VCMresponseAverage__c = ((dblResponseTotal/totalAnswered))*(20);
                
           }
        }
    }

}